<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/10/25
 * Time: 21:37
 */

namespace app\home\controller;
use app\home\model\WechatUser;
use think\Db;
use app\home\model\Xuexiao;
use app\admin\model\Translate as Model;
use com\wechat\TPWechat;
use think\Config;
/**
 * Class Translate
 * @package app\home\controller
 * 每日翻译
 */
class Translate extends Base
{
    /*
     * 首页
     */
    public function index(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role')->find();
        if ($User['review_status'] == 0 || $User['subscribe'] == 0 || $User['role'] == 1){
            $this->redirect('Journal/null');
        }
        $this->jssdk();
        // 学生
        $info = [];
        $type = 1; // 1 考研 2 专升本 3 四六级 4 雅思 5 托福
        $translate = Db::name('translate')->where(['type' => $type,'create_time' => strtotime(date('Y-m-d'))])->field('title,id,content,create_time')->find();
        $is = 0;
        $info['id'] = 0;
        $info['userid'] = $userId;
        if ($translate){
            $is = 1;
            // 今日有内容添加
            $info['title'] = $translate['title'];
            $info['id'] = $translate['id'];
            $info['content'] = $translate['content'];
            $info['create_time'] = date('Y-m-d',$translate['create_time']);
            $answer = Db::name('translate_answer')->where(['openid' => $userId,'tid' => $translate['id']])->field('content,result')->find();
            if (empty($answer)){
                // 今日作业没有完成
                $info['answer'] = '';
                $info['result'] = '';
            }else{
                // 今日作业已经提交
                $info['answer'] = $answer['content'];
                $info['result'] = $answer['result'] ? $answer['result'] : '暂无';
            }
        }
        $this->assign('info',$info);
        $this->assign('is',$is);
        return $this->fetch();

    }
    // 内容 提交
    public function sign(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role')->find();
        if ($User['review_status'] == 0 || $User['subscribe'] == 0 || $User['role'] == 1){
            $this->redirect('Journal/null');
        }
        $answer = input('answer');
        $id = input('id');
        $userid = input('userid');
        $res = Db::name('translate_answer')->insert(['tid' => $id,'openid' => $userid,'content' => $answer,'create_time' => time()]);
        if ($res){
            return $this->success('提交成功');
        }else{
            return $this->error('提交失败');
        }
    }
    // 作业列表记录
    public function personal(){
        $userId = input('openid',session('userId'));
        $User = WechatUser::where(['openid' => $userId])->find();
        if ($User['review_status'] == 0 || $User['role'] == 1 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $msg = array();
        $msg['openid'] = $User['openid'];
        $msg['header'] = $User['headimgurl'];  // 头像
        if (empty($User['name'])){
            $msg['name'] = $User['nickname'];
        }else{
            $msg['name'] = $User['name'];  // 姓名
        }
        $msg['xuexiao'] = '暂无';
        if ($User['school_id']){
            $school = Xuexiao::where('dx_id',$User['school_id'])->find();
            if ($school){
                $msg['xuexiao'] = iconv("gbk", "utf-8//ignore", $school['dx']);
            }
        }
        if (input('openid')){
            $this->assign('is',1);
        }else{
            $this->assign('is',0);
        }
        if (IS_POST){
            $year = input('year');
            $month = input('month');
            //只选取年份，默认为所有
            if($year && ($month == 0)) {
                $info = array(
                    "FROM_UNIXTIME(create_time,'%Y')" => $year,
                    'openid' => $userId,
                );
            }else{
                if ($month < 10){
                    $month = "0".$month;
                }
                $info = array(
                    "FROM_UNIXTIME(create_time,'%Y%m')"  => $year.$month,
                    'openid' => $userId,
                );
            }
            $all = Db::name('translate_answer')->where($info)->order('create_time desc')->field('tid,id,result,create_time')->select();
            //重组用key值分组
            $list = array();
            foreach ($all as $value) {
                $k = date("n",$value['create_time']);
                $title = Model::where('id',$value['tid'])->value('title');
                $scan = 0;
                if ($value['result']){
                    $scan = 1;
                }
                $list[$k][] = array('id' => $value['id'],'day' => date("d", $value['create_time']),'title' => $title,'scan' => $scan);
            }
            krsort($list);
            return $this->success('','',$list);
        }else{
            $msg['year'] = date('Y',time());
            $msg['mouth'] = date('m',time());
            //获取全部共同数据年份
            $years = Model::all(function($query){
                $query->group("FROM_UNIXTIME(create_time,'%Y')")->field("FROM_UNIXTIME(create_time,'%Y') as year");
            });
            array_shift($years);
            $this->assign('years',$years);
        }
        if ($msg['mouth'] < 10){
            $msg['mouth'] = substr($msg['mouth'],1);
        }
        $this->assign('msg',$msg);
        return $this ->fetch();
    }

    //日志详情
    public function detail(){
        $id = input('id/d');
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('review_status,subscribe,role')->find();
        if ($User['review_status'] == 0 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $info = [];
        $msg = Db::name('translate_answer')->where(['id' => $id])->field('tid,content,result')->find();
        $translate = Db::name('translate')->where(['id' => $msg['tid']])->field('title,content,create_time')->find();
        $info['title'] = $translate['title'];
        $info['id'] = $id;
        $info['content'] = $translate['content'];
        $info['answer'] = $msg['content'];
        $info['result'] = $msg['result'] ? $msg['result'] : '';
        $info['create_time'] = date('Y-m-d',$translate['create_time']);
        if ($User['role'] == 1){
            // 老师
            $this->assign('is',1);
        }else{
            // 学生
            $this->assign('is',0);
        }
        $this->assign('info',$info);
        return $this ->fetch();
    }
    // 批阅 提交
    public function signs(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role')->find();
        if ($User['review_status'] == 0 || $User['subscribe'] == 0 || $User['role'] == 0){
            $this->redirect('Journal/null');
        }
        $answer = input('answer');
        $id = input('id');
        $res = Db::name('translate_answer')->where(['id' => $id])->update(['teacherid' => $userId,'result' => $answer]);
        if ($res){
            $openid = Db::name('translate_answer')->where('id',$id)->value('openid');
            $con = '【批阅更新了】
你的翻译已评阅，<a href="http://mp.csppat.com/DDDeducation/public/index.php/home/translate/detail/id/'.$id.'">_点我_</a>可快速查看老师的批注哦~~';
            $send = array(
                "content" => $con
            );
            $Wechat = new TPWechat(Config::get('party'));
            $data = array(
                "touser" => $openid,
                "msgtype" => "text",
                "text" => $send
            );
            $Wechat->sendCustomMessage($data);
            return ['code' => 1];
        }else{
            return ['code' => 0];
        }
    }
    // 删除
    public function dele(){
        $id = input('id');
        if (empty($id)){
            return $this->error('系统参数错误');
        }
        $res = Db::name('translate_answer')->where(['id' => $id])->delete();
        if ($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}